package com.ciwong.epaper.modules.scan.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.common.guidedialog.ChooseReadDialog;
import com.ciwong.epaper.modules.epaper.bean.EpaperInfo;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.epaper.bean.ModuleContent;
import com.ciwong.epaper.modules.epaper.bean.ModuleInfo;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.epaper.dao.EpaperRequest;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.epaper.modules.epaper.util.MultipleServicesDialog;
import com.ciwong.epaper.modules.interactivetutorial.InteractiveTutorialCordovaActivity;
import com.ciwong.epaper.modules.me.bean.CheckValidBean;
import com.ciwong.epaper.modules.me.bean.ServiceDetail;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.modules.onlineanswer.OnlineAnswerCordovaActivity;
import com.ciwong.epaper.modules.scan.adapter.BaseAdapter;
import com.ciwong.epaper.modules.scan.bean.QRCodeDetail;
import com.ciwong.epaper.modules.scan.bean.QRCodeInfo;
import com.ciwong.epaper.modules.viedoexplantion.util.ViedoJumpManager;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.DialogUtil;
import com.ciwong.epaper.util.ESystem;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.epaper.util.download.DownLoadInfo;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.libs.widget.CWToast;
import com.ciwong.mobilelib.bean.Main;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.ciwong.mobilelib.utils.ViewHolder;
import com.ciwong.mobilelib.widget.CWDialog;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 电子报二维码详情
 *
 * @author bin
 * @version ciwong v.1.0 2016/3/30 10:31
 * @since ciwong v.1.0
 */
public class EpaperQRDetailActivity extends BaseActivity {
    private DownLoadInfo downLoadInfo;

    private QRCodeInfo qrCodeInfo;

    private List<QRCodeDetail> qrCodeList;

    private ListView list_view;
    private RelativeLayout no_data_qr;

    private ImageView mIvCover;

    private TextView mTvResourceName;

    private View contain;

    private ChooseReadDialog dialog;

    private String mKey;

    private List<EpaperInfo.Server> serverList;

    private List<ServiceDetail> serviceDetails;

    private ServiceDetail detailSer;

    private CWDialog cofigDialog;
    private int serviceId;

    private long effectiveDate = -1;

    private EApplication eApplication;
    private ServiceDetail mServiceDetail;
    /**
     * 是否资源预览
     */
    boolean isPreView;

    private boolean isCallBack = true;

    private static final String TAG = "EpaperQRDetailActivity";
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            isCallBack = true;

        }
    };

    @Override
    protected void preCreate() {
        try {
            Intent intent = getIntent();
            if (intent != null) {
                downLoadInfo = (DownLoadInfo) intent
                        .getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);
                mKey = intent.getStringExtra(IntentFlag.INTENT_FLAG_STR);
                serverList = (List<EpaperInfo.Server>) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ_LIST);


            }
        } catch (Exception e) {
            e.getStackTrace();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        findLocServer();
    }

    @Override
    protected int setView() {
        return R.layout.activity_newspaper_qrdetail;
    }

    @Override
    protected void findViews() {
        try {
            list_view = (ListView) findViewById(R.id.lv_list);
            no_data_qr = (RelativeLayout) findViewById(R.id.no_data_qr);
            View header = getLayoutInflater()
                    .inflate(R.layout.headview_newspaper_qrdetail, null);
            mIvCover = (ImageView) header.findViewById(R.id.iv_cover);
            mTvResourceName = (TextView) header.findViewById(R.id.tv_resouce_name);
            contain = header.findViewById(R.id.contain);
            list_view.addHeaderView(header);
        } catch (Exception e) {
            e.getStackTrace();
        }

    }

    @Override
    protected void initEvent() {
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                try {
                    showCricleProgress();
                    final Activity act = EpaperQRDetailActivity.this;
                    final QRCodeDetail qrCodeDetail = (QRCodeDetail) parent.getItemAtPosition(position);
                    if (qrCodeDetail == null) {
                        return;
                    }
                    final Module module = new Module();
                    //自己构造的module posi 固定为0，
                    final int posi = 0;
                    ModuleInfo moduleInfo = new ModuleInfo();
                    moduleInfo.setModuleId(qrCodeDetail.getModuleId());
                    moduleInfo.setModuleName(qrCodeDetail.getResourceName());
                    module.setModuleInfo(moduleInfo);
                    List<ModuleContent> moduleContentList = new ArrayList<ModuleContent>();
                    final ModuleContent moduleContent = new ModuleContent();
                    moduleContent.setVersionId(qrCodeDetail.getVersionId());

                    if (TextUtils.isEmpty(qrCodeDetail.getParentVersionId())) {
                        moduleContent.setParentVersionId("0");
                    } else {
                        moduleContent.setParentVersionId(
                                qrCodeDetail.getParentVersionId());
                    }
                    moduleContent.setResourceType(qrCodeDetail.getResourceType());
                    moduleContent.setResourceFile(qrCodeDetail.getResourceFile());
                    moduleContent.setRefAnswerFile(qrCodeDetail.getRefAnswerFile());
                    moduleContent.setResourceName(qrCodeDetail.getResourceName());
                    moduleContentList.add(moduleContent);
                    module.setResourceList(moduleContentList);

                    if (!NetworkUtils.isOnline()) {
                        ToastUtil.INSTANCE.toastCenterNoNetError();
                        hideCricleProgress();
                        return;
                    }
                    if (isPreView) {
                        checkValidSuccess(posi, act, qrCodeDetail, module, moduleContent);
                        return;
                    }

                    isCallBack = false;


                    EpaperRequest.checkValid(EApplication.BRAND_ID + "", "", downLoadInfo.getBookId(), downLoadInfo.getChapterId(), null,  new BaseExtCallBack(EpaperQRDetailActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
                        @Override
                        public void failed(int errorCode, Object data) {
                            super.failed(errorCode, data);
                            hideCricleProgress();
                            mHandler.sendEmptyMessageDelayed(0, 2000);
                        }

                        @Override
                        public void failed(Object data) {
                            super.failed(data);
                            showToastError(data.toString());
                            hideCricleProgress();
                            mHandler.sendEmptyMessageDelayed(0, 2000);
                        }

                        @Override
                        public void success(Object data) {
                            if (data == null) {
                            } else {
                                final CheckValidBean checkValidBean = (CheckValidBean) data;
                                if (checkValidBean != null && checkValidBean.isValid == 1 ||
                                        isPreView ||
                                        qrCodeDetail.getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN ||
                                        qrCodeDetail.getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN_NEW) { //有权限

                                    checkValidSuccess(posi, act, qrCodeDetail, module, moduleContent);

                                } else { //没有权限
                                    MultipleServicesDialog.OnServicesItemClick onServicesItemClick = new MultipleServicesDialog.OnServicesItemClick() {
                                        @Override
                                        public void onServicesItemClick(int position) {
                                            MeJumpManager.jumpBuyService(EpaperQRDetailActivity.this, R.string.go_back, checkValidBean.services.get(position).id, -1, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_PAY, checkValidBean.msg);

                                        }
                                    };

                                    if (progressBar.isShowing()) {
                                        MultipleServicesDialog multipleServicesDialog = new MultipleServicesDialog(EpaperQRDetailActivity.this, checkValidBean.services, onServicesItemClick);

                                        try {//有可能已经退出扫码界面，才请求到权限。防止崩溃。

                                            if (checkValidBean.services.size() == 1) {

                                                MeJumpManager.jumpBuyService(EpaperQRDetailActivity.this, R.string.go_back, checkValidBean.services.get(0).id, -1, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_PAY, checkValidBean.msg);
                                            } else {
                                                multipleServicesDialog.show();
                                                multipleServicesDialog.setTitle(checkValidBean.msg);
                                            }

                                        } catch (Exception e) {
                                            e.getStackTrace();
                                        } finally {

                                        }
                                    }
                                    hideCricleProgress();
                                }
                            }

                            mHandler.sendEmptyMessageDelayed(0, 2000);
                        }
                    });
                } catch (Exception e) {
                    e.getStackTrace();
                }


            }
        });
    }


    private void findServerId() {
        try {
            if (detailSer == null) {
                detailSer = compServer();
                serviceId = detailSer == null ? 0 : detailSer.getServiceType();
            }
        } catch (Exception e) {
            e.getStackTrace();
        }


    }


    private void checkValidSuccess(int position, Activity act, QRCodeDetail qrCodeDetail, Module module, ModuleContent moduleContent) {

        try {
            findServerId();
            // TODO: 2018/3/22  lzh 去掉 下面的逻辑判断

            /*if (qrCodeDetail.getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN &&
                    qrCodeDetail.getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN_NEW && !isPreView && serviceId < 1) {
                ToastUtil.INSTANCE.toastInCenter(EpaperQRDetailActivity.this,"找不到匹配的有效服务");
                return;
            }
*/

            switch (qrCodeDetail.getModuleId()) {
                // 时文
                case ModuleInfo.ModuleInfoId.MODULE_ID_ESSAY:
                    if (progressBar.isShowing()) {
                        MeJumpManager.jumpToTimeNewsH5Activity(R.string.go_back, act, downLoadInfo, module, position);
                        hideCricleProgress();
                    }
                    break;
                // 同步测试
                case ModuleInfo.ModuleInfoId.MODULE_ID_TEST:
                    if (progressBar.isShowing()) {


                        String packagesJsonPath = ESystem
                                .getPackagesJsonPath(
                                        qrCodeDetail.getResourceFile());
                        if (new File(packagesJsonPath).exists()) {
                            EpaperJumpManager.jumpToDictaion(
                                    R.string.go_back, act, downLoadInfo,
                                    module, position);
                        } else {
                            showToastSuccess(R.string.offline_answer);
                        }
                        hideCricleProgress();
                    }
                    break;

                // 听说模考
                case ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM:
                    if (progressBar.isShowing()) {
                        if (DialogUtil.sdIsFull(downLoadInfo)) {
                            DialogUtil.showSdFullDialog(
                                    EpaperQRDetailActivity.this);
                            return;
                        }
                        if (isPreView) {
                            jumpToListenSpeakWorkPreView(act, module, position);
                        } else {
                            jumpToListenSpeakWork(module, position);
                        }
                        hideCricleProgress();
                    }
                    break;
                // 同步跟读
                case ModuleInfo.ModuleInfoId.MODULE_ID_SENTENCE_REPEAT:
                case ModuleInfo.ModuleInfoId.MODULE_ID_REPEAT:
                    if (progressBar.isShowing()) {
                        if (DialogUtil.sdIsFull(downLoadInfo)) {
                            DialogUtil.showSdFullDialog(
                                    EpaperQRDetailActivity.this);
                            return;
                        }
                        dialog = new ChooseReadDialog(act, "0",
                                downLoadInfo, module, position,
                                qrCodeDetail.getResourceType(), -1, serviceId,-1);
                        dialog.show();
                        hideCricleProgress();
                    }

                    break;
                // 报听写
                case ModuleInfo.ModuleInfoId.MODULE_ID_DICTATION:
                    if (progressBar.isShowing()) {
                        String classId = "";
                        if (EApplication.getInstance().getClazz() != null) {
                            classId = String.valueOf(EApplication.getInstance().getClazz().getClassId());
                        }
                        EpaperJumpManager.jumpToDictaionNew(R.string.go_back,
                                act, "0", downLoadInfo, module, position, -1, classId, null, -1, false, null,effectiveDate);
                        hideCricleProgress();
                    }
                    break;
                //视频讲解
                case ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN:
                case ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN_NEW:
//                        视频到里面去再判断权限
                    loadMainData(downLoadInfo, qrCodeDetail.getModuleId(), qrCodeDetail.getVersionId(), serviceId, moduleContent);
                    break;
                //线上作答
                case ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER:
                    loadMainData(downLoadInfo, ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER, qrCodeDetail.getVersionId(), serviceId, moduleContent);
                    break;
                //互动教程
                case ModuleInfo.ModuleInfoId.MODULE_ID_INTERACTIVE_TUTORIAL:
                    loadMainData(downLoadInfo, ModuleInfo.ModuleInfoId.MODULE_ID_INTERACTIVE_TUTORIAL, qrCodeDetail.getVersionId(), serviceId, moduleContent);
                    break;
                default:
                    showToastSuccess(R.string.offline_answer);
                    hideCricleProgress();
                    break;
            }

        } catch (Exception e) {
            e.getStackTrace();
            hideCricleProgress();
        }


    }


    private void findLocServer() {
        try {
            SerializableManager.getInstance().deSerialize(SerializableManager.SerializeKey.SHARE_KEY_SERVICE_DETAIL_LIST, new BaseCallBack() {

                @Override
                public void failed(int errorCode, Object data) {

                }

                @Override
                public void failed(Object data) {

                }

                @Override
                public void success(Object data) {
                    serviceDetails = (List<ServiceDetail>) data;
                }
            });
        } catch (Exception e) {
            e.getStackTrace();
        }


    }


    /**
     * 判断该书籍所属服务以及服务状态
     *
     * @return
     */
    private ServiceDetail compServer() {
        try {
            if (serviceDetails != null && serverList != null) {
                for (ServiceDetail item : serviceDetails) {
                    for (EpaperInfo.Server ser : serverList) {
                        if (item.getServiceType() == ser.getId()) {
                            if (item.isVip() || !item.isbExpired()) {
                                return item;
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.getStackTrace();
        }

        return null;
    }

    /**
     * 资源预览，不弹出dialog模式选择。
     *
     * @param act
     * @param module
     * @param position
     */
    private void jumpToListenSpeakWorkPreView(final Activity act, final Module module,
                                              final int position) {

        try {
            String path = ESystem.getPackagesMainJsonPath(downLoadInfo.getBookId(),
                    downLoadInfo.getChapterId());
            EpaperDao.getInstance().getSerializableObjects(path, Main.class,
                    new BaseExtCallBack(EpaperQRDetailActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {

                        @Override
                        public void failed(Object data) {
                            showToastError((int) data);
                        }

                        @Override
                        public void failed(int errorCode, Object data) {
                            super.failed(errorCode, data);
                        }

                        @Override
                        public void success(Object data) {
                            Main main = (Main) data;
                            int lsMode = ListenSpeakUtil.LsMode.OWN_PRACTISE;

                            //serviceid  预览模式下只能乱给
                            EpaperJumpManager.jumpToListenSpeakWork(R.string.go_back, (BaseActivity) act, "0", downLoadInfo, module, position, null,
                                    main.getJsonVersion(), "", lsMode, -1, null, 0, 0,false);

                        }
                    });
        } catch (Exception e) {
            e.getStackTrace();
        }


    }

    /**
     * 读取main文件跳转到听说模考
     *
     * @param module
     * @param position
     */
    private void jumpToListenSpeakWork(final Module module,
                                       final int position) {
        try {
            String path = ESystem.getPackagesMainJsonPath(downLoadInfo.getBookId(),
                    downLoadInfo.getChapterId());
            EpaperDao.getInstance().getSerializableObjects(path, Main.class,
                    new BaseExtCallBack(EpaperQRDetailActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {

                        @Override
                        public void failed(Object data) {
                            showToastError((int) data);
                        }

                        @Override
                        public void failed(int errorCode, Object data) {
                            super.failed(errorCode, data);
                        }

                        @Override
                        public void success(Object data) {
                            Main main = (Main) data;
                            EpaperJumpManager.jumpToListenSpeakWork(R.string.go_back,
                                    EpaperQRDetailActivity.this, "0", downLoadInfo, module, position, null,
                                    main.getJsonVersion(), "", ListenSpeakUtil.LsMode.OWN_PRACTISE, serviceId, null, 0, 0,false);

                        }
                    });
        } catch (Exception e) {
            e.getStackTrace();
        }


    }

    @Override
    protected void init() {
        isPreView = getIntent().getBooleanExtra(IntentFlag.INTENT_FLAG_PRE_VIEW, false);
        setTitleText(R.string.qr_info_detail);
    }

    @Override
    protected void loadData() {

        try {
            String path = ESystem.getPackagesMainJsonPath(downLoadInfo.getBookId(),
                    downLoadInfo.getChapterId());

            eApplication = (EApplication) getApplication();

            EpaperDao.getInstance().getSerializableObjects(path, Main.class,
                    new BaseExtCallBack(EpaperQRDetailActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {

                        @Override
                        public void failed(Object data) {
                            showToastError((int) data);
                        }

                        @Override
                        public void failed(int errorCode, Object data) {
                            super.failed(errorCode, data);
                        }

                        @Override
                        public void success(Object data) {
                            Main main = (Main) data;
                            getQRCodeInfo(main.getQrCodeFile());
                        }
                    });
        } catch (Exception e) {
            e.getStackTrace();
        }


    }

    /**
     * 获取目录内容信息
     *
     * @param path 目录内容信息地址
     */
    private void getQRCodeInfo(String path) {
        try {
            path = ESystem.getPackagesPath(EpaperQRDetailActivity.this) + File.separator + path;
            Type responseType = new TypeToken<List<QRCodeInfo>>() {
            }.getType();
            EpaperDao.getInstance().getSerializableObjects(path, responseType,
                    new BaseExtCallBack(EpaperQRDetailActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {

                        @Override
                        public void failed(Object data) {
                            showToastError((int) data);
                        }

                        @Override
                        public void failed(int errorCode, Object data) {
                            super.failed(errorCode, data);
                        }

                        @Override
                        public void success(Object data) {
                            try {
                                List<QRCodeInfo> qrCodeInfos = (List<QRCodeInfo>) data;
                                //先打包后生成二维码的 二维码有问题  给个提示页面
                                if (null == qrCodeInfos || qrCodeInfos.size() == 0) {
                                    list_view.setVisibility(View.GONE);
                                    no_data_qr.setVisibility(View.VISIBLE);
                                }
                                for (QRCodeInfo qrCodeInfo : qrCodeInfos) {
                                    if (mKey.equals(qrCodeInfo.getUrl())) {
                                        EpaperQRDetailActivity.this.qrCodeInfo = qrCodeInfo;
                                        loadDataForView();
                                        break;
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                    });
        } catch (Exception e) {
            e.getStackTrace();
        }

    }

    private void loadDataForView() {
        try {
            if (downLoadInfo != null) {
                ImageLoader.getInstance().displayImage(
                        downLoadInfo.getIconUrl(),
                        new ImageViewAware(mIvCover));
                mTvResourceName.setText(qrCodeInfo.getCodeName());
                qrCodeList = qrCodeInfo.getResourceList();
                QRCodeDetailAdapter qRCodeDetailAdapter = new QRCodeDetailAdapter(
                        EpaperQRDetailActivity.this, qrCodeList);
                list_view.setAdapter(qRCodeDetailAdapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public class QRCodeDetailAdapter extends BaseAdapter<QRCodeDetail> {

        public QRCodeDetailAdapter(Activity act, List list) {
            super(act, list);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            try {
                View view = null;
                if (convertView == null) {
                    view = View.inflate(EpaperQRDetailActivity.this,
                            R.layout.adapter_content_normal_item, null);
                } else {
                    view = convertView;
                }
                ViewGroup viewGroup = ViewHolder.get(view, R.id.item_content_normal_bg_rl);

                TextView resource_name = ViewHolder.get(view, R.id.resource_name);
                TextView tx_module_name = ViewHolder.get(view, R.id.tx_module_name);
                ImageView img_module_type = ViewHolder.get(view, R.id.img_module_type);
                View img_arrow_right = ViewHolder.get(view, R.id.img_arrow_right);

                QRCodeDetail qrCodeDetail = mList.get(position);

                switch (qrCodeDetail.getModuleId()) {


                    // 同步测试
                    case ModuleInfo.ModuleInfoId.MODULE_ID_TEST:
                        img_module_type.setImageResource(R.mipmap.module_type_test);
                        tx_module_name.setText("单元测试");
                        break;

                    // 时文
                    case ModuleInfo.ModuleInfoId.MODULE_ID_ESSAY:
                        img_module_type.setImageResource(R.mipmap.module_type_essay);
                        tx_module_name.setText("时文赏析");
                        break;

                    //同步训练
                    case ModuleInfo.ModuleInfoId.MODULE_ID_TRAIN:
                        img_module_type.setImageResource(R.mipmap.module_type_train);
                        tx_module_name.setText("同步训练");
                        break;

                    // 同步跟读
                    case ModuleInfo.ModuleInfoId.MODULE_ID_REPEAT:
                        tx_module_name.setText("同步跟读");
                        img_module_type.setImageResource(R.mipmap.module_type_repeat);
                        break;

                    // 听说模考
                    case ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM:
                        tx_module_name.setText("听说模考");
                        img_module_type.setImageResource(R.mipmap.module_type_listen_speak_exam);
                        break;


                    // 微技能训练
                    case ModuleInfo.ModuleInfoId.MODULE_ID_MICRO_SKILLS:
                        tx_module_name.setText("微技能训练");
                        img_module_type.setImageResource(R.mipmap.module_type_micro_skills);
                        break;

                    // 报听写
                    case ModuleInfo.ModuleInfoId.MODULE_ID_DICTATION:
                        tx_module_name.setText("报听写");
                        img_module_type.setImageResource(R.mipmap.module_type_dictation);
                        break;

                    // 视频讲解
                    case ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN:
                        tx_module_name.setText("视频讲解");
                        img_module_type.setImageResource(R.mipmap.module_type_video);
                        break;
                    case ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN_NEW:
                        tx_module_name.setText("视频教程");
                        img_module_type.setImageResource(R.mipmap.module_type_video);
                        break;

                    // 在线作答
                    case ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER:
                        tx_module_name.setText("线上作答");
                        img_module_type.setImageResource(R.mipmap.module_type_online_answer);
                        break;
                    // 互动教程
                    case ModuleInfo.ModuleInfoId.MODULE_ID_INTERACTIVE_TUTORIAL:
                        tx_module_name.setText("互动教程");
                        img_module_type.setImageResource(R.mipmap.module_type_interactive_tutorial);
                        break;

                    default:
                        img_arrow_right.setVisibility(View.GONE);
                        break;
                }


                if (position % 2 == 0) {
                    viewGroup.setBackgroundColor(Color.parseColor("#f9f9f9"));
                } else {
                    viewGroup.setBackgroundColor(Color.parseColor("#f5f5f5"));
                }

                resource_name.setText(qrCodeDetail.getResourceName());

                return view;

            } catch (Exception e) {
                e.getStackTrace();
            }
            return null;
        }

    }


    protected void loadMainData(final DownLoadInfo downLoadInfo, final int moduleId, final String versionId, final int serviceId, final ModuleContent moduleContent) {
        try {
            String path = ESystem.getPackagesMainJsonPath(downLoadInfo.getBookId(),
                    downLoadInfo.getChapterId());
            EpaperDao.getInstance().getSerializableObjects(path, Main.class,
                    new BaseExtCallBack(EpaperQRDetailActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {


                        @Override
                        public void failed(Object data) {
                            CWToast.makeText(EpaperQRDetailActivity.this, (int) data,
                                    Toast.LENGTH_LONG, true)
                                    .setToastType(CWToast.TOAST_TYPE_ERROR).show();
                        }

                        @Override
                        public void failed(int errorCode, Object data) {
                            super.failed(errorCode, data);
                        }

                        @Override
                        public void success(Object data) {
                            Main main = (Main) data;
                            getCatalogueFile(main.getCatalogueFile(), downLoadInfo, moduleId, versionId, serviceId, moduleContent);
                        }
                    });
        } catch (Exception e) {
            e.getStackTrace();
        }

    }


    /**
     * 获取目录内容信息
     *
     * @param path        目录内容信息地址
     * @param jsonVersion
     */
    private Module module;

    private void getCatalogueFile(String path, final DownLoadInfo downLoadInfo, final int moduleId, final String versionId, final int serviceId, final ModuleContent moduleContent) {
        try {
            path = ESystem.getPackagesPath(EpaperQRDetailActivity.this) + File.separator + path;
            Type responseType = new TypeToken<List<Module>>() {
            }.getType();
            EpaperDao.getInstance().getSerializableObjects(path, responseType,
                    new BaseCallBack() {
                        @Override
                        public void failed(Object data) {
                            CWToast.makeText(EpaperQRDetailActivity.this, (int) data,
                                    Toast.LENGTH_LONG, true)
                                    .setToastType(CWToast.TOAST_TYPE_ERROR).show();
                        }

                        @Override
                        public void failed(int errorCode, Object data) {

                        }

                        @Override
                        public void success(Object data) {
                            List<Module> modules = (List<Module>) data;
                            if (modules == null) {
                                return;
                            }
                            int position = 0;
                            if (modules.size() > 0) {
                                module = modules.get(0);
                                a:
                                for (int i = 0; i < modules.size(); i++) {
                                    module = modules.get(i);
                                    for (int j = 0; j < module.getResourceList().size(); j++) {
                                        if (module.getResourceList().get(j).getVersionId().equals(versionId)) {
                                            module = modules.get(i);
                                            position = j;
                                            break a;
                                        }
                                    }
                                }
                                if (progressBar.isShowing()) {
                                    if (moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN || moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN_NEW) {
                                        ViedoJumpManager.jumpToNewVideoExplationActivityPreView(R.string.go_back, EpaperQRDetailActivity.this, downLoadInfo, module, position, null, module.getResourceList().get(position).getResourceName(), 0, isPreView, null);
                                    }
                                    if (moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_ONLINE_ANSWER) {//在线作答
                                        MeJumpManager.jumpToOnlineAnswerCordovaActivity2(EpaperQRDetailActivity.this, moduleContent, module, downLoadInfo, position, serviceId, OnlineAnswerCordovaActivity.FROM_DESK, "", 0, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_DO_WORK);
                                    }
                                    if (moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_INTERACTIVE_TUTORIAL) {//互动教程
                                        MeJumpManager.jumpToInteractiveTutorialCordovaActivity(EpaperQRDetailActivity.this, moduleContent, module, downLoadInfo, position, serviceId, InteractiveTutorialCordovaActivity.FROM_DESK, "", 0, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_DO_WORK);
                                    }
                                    hideCricleProgress();
                                }

                            }
                        }
                    });
        } catch (Exception e) {
            e.getStackTrace();
        }


    }


}
